package logica;


import java.awt.Color;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Collection;
import java.util.LinkedList;

import electro.Electrocardiograma;
import electro.Muestra;
import electro.Toma;

// Idea para dsp: Implementar patron observer para avisar 
// cuando haya terminado de adquirir datos ;)

public class Adquisidor_Electro implements Adquisidor_de_Datos {

	private Electrocardiograma elec;
//	private Electrocardiograma elec1;
//	private Electrocardiograma elec2;
//	private Electrocardiograma elec3;
	
	private Toma toma;

	// Constructor
	public Adquisidor_Electro(){
		elec = new Electrocardiograma(); 
		toma = new Toma();
//		elec1 = new Electrocardiograma(); 
//		elec2 = new Electrocardiograma(); 
//		elec3= new Electrocardiograma(); 
	} 

	public void empezar_adquisicion_muestras() {
		BufferedReader br = null;

		try {

			br = new BufferedReader(new FileReader("ecg.txt"));
			String line = null;
				
			//elec = this.elec1;
			//elec1.setColor(Color.black);
			toma.setColor(Color.black);
			while ((line = br.readLine()) != null) {
				if (line.compareTo("00")!= 0){
					toma.agregar_muestra(new Muestra(Integer.parseInt(line)));
				}else{
					if(toma.cant_muestras()>=1){
						elec.agregar_toma(toma);
						
					}
					toma = new Toma();
					if (elec.cant_tomas()==1){
						toma.setColor(Color.magenta);	
					}
					if (elec.cant_tomas()==2){
						toma.setColor(Color.blue);	
					}					
					
//					if(elec2.cant_muestras()<1){
//						elec =  this.elec2;
//						elec2.setColor(Color.magenta);
//					}else{
//						elec = this.elec3;
//						elec3.setColor(Color.blue);
//					}
				}

			}

		}
		catch (FileNotFoundException ex) {
			ex.printStackTrace();
		}
		catch (IOException ex) {
			ex.printStackTrace();
		}
		finally {
			try {
				if (br != null) br.close();
			}
			catch (IOException ex) {
				ex.printStackTrace();
			}

		} // Fin finally

	}

	public void terminar_adquisicion_muestras() {

	}

	public Electrocardiograma da_electro(){
		return elec;
	}
	
//	public Collection<Electrocardiograma> electros(){
//		Collection<Electrocardiograma> electros = new LinkedList<Electrocardiograma>();
//		electros.add(this.elec1);
//		electros.add(this.elec2);
//		electros.add(this.elec3);
//		return electros;
//	}

}
